{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2b164039-55a3-4325-a681-2cf7db30ce48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8.83883476]\n",
      "[-0.35355339]\n",
      "[ 4.  -3.5]\n",
      "[-2.82842712  0.         -4.94974747 -1.41421356]\n"
     ]
    }
   ],
   "source": [
    "# Multilevel DWT decomposition\n",
    "import pywt\n",
    "x = [3, 7, 1, 1, -2, 5, 4, 6]\n",
    "db1 = pywt.Wavelet('db1')\n",
    "cA3, cD3, cD2, cD1 = pywt.wavedec(x, db1)\n",
    "print(cA3)\n",
    "print(cD3)\n",
    "print(cD2)\n",
    "print(cD1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2fa087ff-66b9-480f-8d89-b29f1d123c80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pywt.dwt_max_level(len(x), db1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e002790a-8121-4561-ac9e-76aa29a79988",
   "metadata": {},
   "outputs": [],
   "source": [
    "cA2, cD2, cD1 = pywt.wavedec(x, db1, mode='constant', level=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "51c946f5-5def-4b28-917f-a9cee23f71a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3.  7.  1.  1. -2.  5.  4.  6.]\n"
     ]
    }
   ],
   "source": [
    "# Multilevel IDWT reconstruction\n",
    "coeffs = pywt.wavedec(x, db1)\n",
    "print(pywt.waverec(coeffs, db1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "68a02b35-c25f-46bf-ac38-1d72ac5458c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7.07106781 5.65685425 2.82842712 0.70710678 2.82842712 7.07106781\n",
      " 7.07106781 6.36396103]\n",
      "[-2.82842712  4.24264069 -1.41421356  3.53553391 -5.65685425  1.41421356\n",
      " -1.41421356  2.12132034]\n",
      "[ 7.   4.5  4.   5.5  7.   9.5 10.   8.5]\n",
      "[ 3.   3.5  0.  -4.5 -3.   0.5  0.   0.5]\n"
     ]
    }
   ],
   "source": [
    "# Multilevel SWT decomposition\n",
    "x = [3, 7, 1, 3, -2, 6, 4, 6]\n",
    "(cA2, cD2), (cA1, cD1) = pywt.swt(x, db1, level=2)\n",
    "print(cA1)\n",
    "print(cD1)\n",
    "print(cA2)\n",
    "print(cD2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d31c0e3f-4869-41f3-b9c1-a9325ba06a91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 7.   4.5  4.   5.5  7.   9.5 10.   8.5]\n",
      "[ 3.   3.5  0.  -4.5 -3.   0.5  0.   0.5]\n"
     ]
    }
   ],
   "source": [
    "[(cA2, cD2)] = pywt.swt(cA1, db1, level=1, start_level=1)\n",
    "print(cA2)\n",
    "print(cD2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "87db5cc5-1a87-40ab-a669-533ca68aeb41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coeffs = pywt.swt(x, db1)\n",
    "len(coeffs)\n",
    "pywt.swt_max_level(len(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e45fda58-19af-432a-8efa-5c936da174b7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
